package controller.ur;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import controller.CUtil;
import controller.ur.note.SNoteSave;

import model.dao.UserDaoLocal;
import model.entity.User;

/**
 * Servlet implementation class SAuthorFollowRemove
 */
public class SAuthorFollowRemove extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	private UserDaoLocal userDao;
	
	private Logger log=Logger.getLogger(SAuthorFollowRemove.class);
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SAuthorFollowRemove() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!CUtil.checkLogIn(request, response))
			return;
		
		User user=(User)request.getSession().getAttribute("user");
		int currentUserId=Integer.parseInt((String)request.getParameter("userId"));
		
		userDao.removeFollower(currentUserId, user.getId());
		request.getSession().setAttribute("user", userDao.findById(user.getId()));
		
		log.info("Author: "+user.toString()+" follows "+currentUserId);
		CUtil.redirecter(request, response, "SAuthorsProfile?userId="+currentUserId);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
